<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Map;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Cookie;

class MapController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        if(!request()->has('_selector')){
            $url = admin_url('map?_selector[type]=0');
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: '.$url);
            exit;
        }
        $_selector = request()->get('_selector');
        $map_type = array_get($_selector,'type',0);
        Cookie::queue('selected_map_type',$map_type);
        return Grid::make(new Map(), function (Grid $grid) {

            $grid->column('img')->image('',50,50);
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->showQuickEditButton();
            $grid->disableEditButton();
            $grid->enableDialogCreate();

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('type', '類型', [0=>'名稱貼圖',1=>'三維貼圖']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Map(), function (Show $show) {
            $show->field('id');
            $show->field('img');
            $show->field('status');
            $show->field('type');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Map(), function (Form $form) {

            $form->image('img')->autoUpload()->uniqueName()->move('map');
            $form->switch('status')->default(1);
            $form->hidden('type')->value(Cookie::get('selected_map_type'));

        });
    }
}
